<template>
	<view class="container">
		<view class="list-cell b-b m-t" @click="navTo('persion Info')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{$t('setting.personInfo')}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/address/address')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{$t('setting.addressReceive')}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell" @click="navTo('Real name authentication')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{$t('setting.nameOfTruth')}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		
		<view class="list-cell m-t">
			<text class="cell-tit">{{$t('setting.messagePush')}}</text>
			<switch checked color="#fa436a" @change="switchChange" />
		</view>
		<view class="list-cell m-t b-b" @click="navTo('refresh cache')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{$t('setting.refreshCache')}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<!--
		<view class="list-cell b-b" @click="navToOuter('https://github.com/macrozheng/mall')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">关于mall-app-web</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		-->
		<view class="list-cell">
			<text class="cell-tit">{{$t('setting.checkUpdate')}}</text>
			<text class="cell-tip">{{$t('setting.currentVersion')}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell">
			<text class="cell-tit" @click="changeLang()" hover-class="cell-hover" :hover-stay-time="50">{{$t('setting.language')}}</text>
			<text class="cell-tip">{{$t('setting.currentLanguage')}} {{cur_lang}}</text>
		</view>
		<view class="list-cell">
			<text class="cell-tip">{{$t('home.welcome')}}</text>
			
		</view>
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">{{$t('setting.buttonExit')}} </text>
		</view>
	</view>
</template>

<script>
	import {  
	    mapMutations  
	} from 'vuex';
	let cur_lang = null;
	export default {
		data() {
			return {
			};
		},
		onLoad(){
			this.cur_lang = uni.getStorageSync('system_info.language') ;
			if(this.cur_lang == null){
				this.cur_lang = 'zh';
				uni.setStorageSync('system_info.language' , this.cur_lang);
				this.$i18n.locale = this.cur_lang; // 切换语言
			}
			this.setNavigationBarTitle('pages.setting');
			console.log(this.cur_lang)
		},
		methods:{
			...mapMutations(['logout']),

			navTo(url){
				if(url.indexOf("pages")!=-1){
					uni.navigateTo({
						url:url
					});
				}
				let tip_navi = this.$t('setting.tip_navi_to');
				this.$api.msg(tip_navi + `${url}`);
			},
			navToOuter(url){
				window.location.href = url;
			},
			changeLang() {
				console.log("当前语言" + this.cur_lang);
				
				let new_lang = 'zh' ;
				
				if (this.cur_lang == 'en') {
					new_lang = 'zh' ;
				}
				else if (this.cur_lang == 'zh') {
					new_lang = 'en' ;
				}
				console.log("目标语言" + new_lang);
				uni.setStorageSync('system_info.language' , new_lang);
				this.$i18n.locale = new_lang; // 切换语言
				  
				uni.reLaunch({
					url: '/pages/set/set?locale=' + new_lang
				})
			},
			setNavigationBarTitle(str) {
			    // 假设 this.$i18n.locale 已经设置为当前的语言环境
			    const title = this.$t(str);
				console.log("title of cart" + title);
			    uni.setNavigationBarTitle({ title });
			},
			//退出登录
			toLogout(){
				uni.showModal({
				    content: this.$t('setting.messageExit'), 
				    success: (e)=>{
				    	if(e.confirm){
				    		this.logout();
				    		setTimeout(()=>{
				    			uni.navigateBack();
				    		}, 200)
				    	}
				    }
				});
			},
			//switch
			switchChange(e){
				let tip_open_str = this.$t('setting.tip_open');
				let tip_close_str = this.$t('setting.tip_close');
				let statusTip = e.detail.value ? tip_open_str: tip_close_str;//'关闭';
				this.$api.msg(`${statusTip} ` + this.$t('setting.tip_msg_push'));
			},

		}
	}
</script>

<style lang='scss'>
	page{
		background: $page-color-base;
	}
	.list-cell{
		display:flex;
		align-items:baseline;
		padding: 20upx $page-row-spacing;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: $uni-color-primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right:10upx;
		}
		.cell-tip{
			font-size: $font-base;
			color: $font-color-light;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
	}
</style>
